/**
 * 3η ΓΕ 2013-2014 - Super Market
 * ΦΡΑΓΚΟΥΛΟΠΟΥΛΟΥ ΕΜΜΑΝΟΥΕΛΑ (69298)
 * ΧΑΛΔΑΣ ΧΑΡΑΛΑΜΠΟΣ (69305)
 * ΧΟΡΜΟΒΙΤΗΣ ΙΩΑΝΝΗΣ (69325)
 * ΧΟΥΣΤΟΥΛΑΚΗ ΕΥΑΓΓΕΛΙΑ (69327)
 */
package customerGUI;

import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.YES_NO_OPTION;
import static javax.swing.JOptionPane.YES_OPTION;
import model.*;
import supermarket.EntityManagerFactory;
import supermarket.LocalBanks;
import supermarket.MainForm;

/**
 * Οθόνη Διαχείριση Προφίλ
 */
public class EditProfileForm extends javax.swing.JInternalFrame {

    private MainForm mf;
    private Customer myCustomer;

    /**
     * Creates new form CustomerForm
     */
    public EditProfileForm(MainForm mf) {
        initComponents();
        this.mf = mf;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SuperMarketPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("SuperMarketPU").createEntityManager();
        ProfilePanel = new javax.swing.JPanel();
        updateProfileButton = new javax.swing.JButton();
        deleteProfileButton = new javax.swing.JButton();
        lastNameLabel = new javax.swing.JLabel();
        lastName = new javax.swing.JTextField();
        firstNameLabel = new javax.swing.JLabel();
        firstName = new javax.swing.JTextField();
        addressLabel = new javax.swing.JLabel();
        address = new javax.swing.JTextField();
        creditCardPanel = new javax.swing.JPanel();
        bankLabel = new javax.swing.JLabel();
        bankCombo = new javax.swing.JComboBox();
        creditCardLabel = new javax.swing.JLabel();
        cvvLabel = new javax.swing.JLabel();
        cvv = new javax.swing.JFormattedTextField();
        ownerLabel = new javax.swing.JLabel();
        holdersName = new javax.swing.JTextField();
        creditCard = new javax.swing.JFormattedTextField();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Διαχείριση προφίλ ");

        updateProfileButton.setText("Τροποποίηση");
        updateProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateProfileButtonActionPerformed(evt);
            }
        });

        deleteProfileButton.setText("Διαγραφή");
        deleteProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteProfileButtonActionPerformed(evt);
            }
        });

        lastNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lastNameLabel.setText("Επώνυμο");

        lastName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        firstNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        firstNameLabel.setText("Όνομα");

        firstName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        addressLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        addressLabel.setText("Διεύθυνση");

        address.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        creditCardPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "ΣΤΟΙΧΕΙΑ ΠΙΣΤΩΤΙΚΗΣ ΚΑΡΤΑΣ", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 3, 12))); // NOI18N

        bankLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        bankLabel.setText("Τράπεζα");

        bankCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        bankCombo.setSelectedIndex(-1);
        bankCombo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        creditCardLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        creditCardLabel.setText("Αριθμός Πιστωτικής Κάρτας");

        cvvLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cvvLabel.setText("CVV");

        cvv.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cvv.setColumns(3);
        try {
            cvv.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        ownerLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ownerLabel.setText("Όνομα κατόχου");

        holdersName.setColumns(40);
        holdersName.setAutoscrolls(false);
        holdersName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        creditCard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        creditCard.setColumns(16);
        try {
            creditCard.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-######-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        org.jdesktop.layout.GroupLayout creditCardPanelLayout = new org.jdesktop.layout.GroupLayout(creditCardPanel);
        creditCardPanel.setLayout(creditCardPanelLayout);
        creditCardPanelLayout.setHorizontalGroup(
            creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(creditCardPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(holdersName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 379, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(creditCardPanelLayout.createSequentialGroup()
                        .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(ownerLabel)
                            .add(creditCardPanelLayout.createSequentialGroup()
                                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(bankCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(bankLabel))
                                .add(18, 18, 18)
                                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                                    .add(creditCardLabel)
                                    .add(creditCard, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(18, 18, 18)
                        .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(cvvLabel)
                            .add(cvv, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        creditCardPanelLayout.setVerticalGroup(
            creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(creditCardPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bankLabel)
                    .add(creditCardLabel)
                    .add(cvvLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bankCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(creditCard, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cvv, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(ownerLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(holdersName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout ProfilePanelLayout = new org.jdesktop.layout.GroupLayout(ProfilePanel);
        ProfilePanel.setLayout(ProfilePanelLayout);
        ProfilePanelLayout.setHorizontalGroup(
            ProfilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ProfilePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(ProfilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(ProfilePanelLayout.createSequentialGroup()
                        .add(deleteProfileButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(updateProfileButton))
                    .add(ProfilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(lastNameLabel)
                        .add(lastName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 420, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(firstNameLabel)
                        .add(ProfilePanelLayout.createSequentialGroup()
                            .add(10, 10, 10)
                            .add(addressLabel))
                        .add(firstName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 420, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(address, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 420, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(creditCardPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ProfilePanelLayout.setVerticalGroup(
            ProfilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ProfilePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(lastNameLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lastName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(firstNameLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(firstName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addressLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(address, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(creditCardPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(ProfilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(updateProfileButton)
                    .add(deleteProfileButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(ProfilePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(ProfilePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Button: Τροποποίηση
     * @param evt 
     */
     private void updateProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateProfileButtonActionPerformed
        if (JOptionPane.showConfirmDialog(this,
                 "Να αποθηκευτούν οι αλλαγές;",
                 "Αποθήκευση", YES_NO_OPTION) == YES_OPTION) {
             saveCustomer();
             dispose();
         }
     }//GEN-LAST:event_updateProfileButtonActionPerformed

     /**
      * Button: Διαγραφή
      * @param evt 
      */
     private void deleteProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteProfileButtonActionPerformed

        if (JOptionPane.showConfirmDialog(this,
                 "Θέλετε να διαγράψετε το προφίλ σας;",
                 "Διαγραφή Προφίλ", YES_NO_OPTION) == YES_OPTION) {
             EntityManager em = EntityManagerFactory.getEntityManager();
             em.getTransaction().begin();
             em.remove(myCustomer);
             em.getTransaction().commit();
             dispose();

             mf.disconnect();
         }
     }//GEN-LAST:event_deleteProfileButtonActionPerformed

     /**
      * Γεμίζει τα πεδία της οθόνης (σε edit mode)
      * @param c 
      */
    public void setCustomer(Customer c) {
        myCustomer = c;
        firstName.setText(myCustomer.getFirstName());
        lastName.setText(myCustomer.getLastName());
        address.setText(myCustomer.getAddress());
        creditCard.setText(myCustomer.getCreditCardNum());
        holdersName.setText(myCustomer.getOwner());
        cvv.setText(myCustomer.getCvv());
        LocalBanks lb = new LocalBanks();
        lb.fillCombo(bankCombo);
        bankCombo.setSelectedIndex(lb.getBankdId(myCustomer.getBank()));
    }

    /**
     * Save Record
     */
    public void saveCustomer() {
        myCustomer.setFirstName(firstName.getText());
        myCustomer.setLastName(lastName.getText());
        myCustomer.setAddress(address.getText());
        myCustomer.setCreditCardNum(creditCard.getText());
        myCustomer.setBank((String) (bankCombo.getSelectedItem()));
        myCustomer.setOwner(holdersName.getText());
        myCustomer.setCvv(cvv.getText());
        EntityManager em = EntityManagerFactory.getEntityManager();
        em.getTransaction().begin();
        em.persist(myCustomer);
        em.getTransaction().commit();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ProfilePanel;
    private javax.persistence.EntityManager SuperMarketPUEntityManager;
    private javax.swing.JTextField address;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JComboBox bankCombo;
    private javax.swing.JLabel bankLabel;
    private javax.swing.JFormattedTextField creditCard;
    private javax.swing.JLabel creditCardLabel;
    private javax.swing.JPanel creditCardPanel;
    private javax.swing.JFormattedTextField cvv;
    private javax.swing.JLabel cvvLabel;
    private javax.swing.JButton deleteProfileButton;
    private javax.swing.JTextField firstName;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField holdersName;
    private javax.swing.JTextField lastName;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel ownerLabel;
    private javax.swing.JButton updateProfileButton;
    // End of variables declaration//GEN-END:variables
}
